#ifndef SQLCONNPOOL_H
#define SQLCONNPOOL_H

#include <mysql/mysql.h>
#include <string>
#include <queue>
#include <mutex>
#include <semaphore.h>
#include <thread>
#include "../log/log.h"

class SqlConnPool {
public:
    static SqlConnPool *Instance(); //单例模式

    MYSQL *GetConn();
    void FreeConn(MYSQL * conn); //释放连接，放回数据库连接池
    int GetFreeConnCount();

    void Init(const char* host, int port,
              const char* user,const char* pwd, 
              const char* dbName, int connSize);
    void ClosePool();

private:
    SqlConnPool();
    ~SqlConnPool();

    int MAX_CONN_; //最大连接数
    int useCount_; //当前用户数
    int freeCount_; //还可以再连接几个用户

    std::queue<MYSQL *> connQue_; //操作mysql数据库
    std::mutex mtx_; //互斥锁
    sem_t semId_; //信号量
};


#endif 